package com.omesoft.hksnore.util.omeview;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.omesoft.hksnore.R;


/**
 * 自定义无边框圆形图片控件
 */
public class OvalHollowImageView extends ImageView {
    static float move = -0.1f;// 圆偏移，正向右，负向左
    // static boolean isBorder;
    // static int borderColor;
    private boolean isBorder;
    private int borderColor;

    public OvalHollowImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public OvalHollowImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.OvalHollow_ImageView);
        isBorder = ta.getBoolean(R.styleable.OvalHollow_ImageView_setBorder, false);
        borderColor = ta.getColor(R.styleable.OvalHollow_ImageView_setBorderColor, 00000000);
        ta.recycle();
    }

    public OvalHollowImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();// 获取imageview里的图片
        if (drawable == null) {
            return;
        }
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        if (b == null) {
            return;
        }
        Bitmap bitmap = b.copy(Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();// 获取imageview中设置的宽度和高度
//		Log.d("test", "onDraw::w::" + w);
        Bitmap roundBitmap = getOvalBitmap(bitmap, w, isBorder, borderColor);
        canvas.drawBitmap(roundBitmap, 0, 0, null);

    }

    @SuppressLint("ResourceAsColor")
    public static Bitmap getOvalBitmap(Bitmap bmp, int radius, boolean isBorder, int borderColor) {
        Bitmap sbmp;
        if (bmp.getWidth() != radius || bmp.getHeight() != radius)
            sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
        else
            sbmp = bmp;
        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        // paint.setStrokeWidth(0);
        canvas.drawCircle(sbmp.getWidth() / 2 + move, sbmp.getHeight() / 2 + move, sbmp.getWidth() / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);

        paint.setColor(00000000);// 设置画笔颜色，直接写颜色代码，从资源获取无效
        paint.setAntiAlias(true); // 消除锯齿
        paint.setStyle(Paint.Style.STROKE); // 绘制空心圆
        canvas.drawCircle(sbmp.getWidth() / 2 + move, sbmp.getWidth() / 2 + move, sbmp.getWidth() / 2, paint);

        // 边框
        if (isBorder) {
            paint.setColor(borderColor);
            paint.setStrokeWidth((float) (sbmp.getWidth() / 2 * 0.10));
            canvas.drawCircle(sbmp.getWidth() / 2 + move, sbmp.getWidth() / 2 + move, sbmp.getWidth() / 2,
                    paint);
        }
        return output;
    }

}